#import "iPhoneOSBuild.h"

#if USE_CORESURFACE

#include <stdio.h>
#include <stdint.h>
#include <pthread.h>

#import "CoreSurface.h"
#import <AudioToolbox/AudioQueue.h>

extern  int     myargc;
extern  char**  myargv;

extern pthread_cond_t screenUpdateLock;
extern pthread_mutex_t screenUpdateMutex;
extern void updateScreen();
extern void D_DoomMain();
extern CoreSurfaceBufferRef screenSurface;

void iPhone_Video_End()
{
}

void iPhone_Video_LoadFrame(unsigned long *fb)
{
	unsigned short *c;
	int x, y, i = 0;
	
	c = CoreSurfaceBufferGetBaseAddress(screenSurface);
	
	pthread_mutex_lock(&screenUpdateMutex);
	
	for (y=0; y < 240; y++)
	{
		for (x=0; x<320; x++) {
			c[(320*y)+x] = fb[(320*y)+x];
		}
	}
	
	pthread_mutex_unlock(&screenUpdateMutex);
	updateScreen();
}

void iPhone_Video_Init()
{
}

void *game_thread(void *args) {
	myargc = 0;
	//myargv = NULL;
	
	char myargs[2];
	
	
	myargs[1] = "-episode 1";

	myargv = myargs;
	
	NSAutoreleasePool * pool = [NSAutoreleasePool new];
	setenv("DOOMWADDIR", [[[NSBundle mainBundle] resourcePath] UTF8String], 1);
	[pool release];	
	
	D_DoomMain ();
}
#endif
